/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-11-09     zzzlion       the first version
 */
#ifndef APPLICATIONS_YSF4PRO_THREADUNIT_H_
#define APPLICATIONS_YSF4PRO_THREADUNIT_H_

#include <rtthread.h>
#include <string>
#include "cJson.h"
#include <map>
using namespace std;

// 定义一个函数指针变量，这个变量可以指向任何符合 void (*methodPrt)(cJSON *json_params) 签名的函数
// Define a function pointer variable that can point to any function that conforms to the void (* methodPrt) (cJSON * json params) signature
typedef void (*methodPrt)(cJSON *json_params); // 定义别名才能放在vector和map中

struct msg
{
    rt_uint8_t *data_ptr;  /* 数据块首地址 */
    rt_uint32_t data_size; /* 数据块大小   */
};

/**
 * @brief Used to create and manage threads on RT Thread. It supports receiving messages, processing JSON data, and calling corresponding functions.
 * 用于在RT-Thread上创建和管理线程。它支持接收消息，处理 JSON 数据，并调用相应的函数。
 */
class ThreadUnit
{
public:
    ThreadUnit();
    virtual ~ThreadUnit();
    int recv(msg);

public:
    const char *FunName; // 函数名称

protected:
    string TIMName = "NULL";
    struct rt_messagequeue message;
    rt_uint8_t msg_pool[32];
    void (*entry)(void *parameter);
    map<string, methodPrt> methodNameList;

protected:
    void init(string name, rt_uint8_t priority);
    void handle(rt_mq_t msg);
    void handleFUn(cJSON *methodName, cJSON *params);
};

#endif /* APPLICATIONS_YSF4PRO_THREADUNIT_H_ */
